package com.zhu.library_management_system.service.Impl;

import com.zhu.library_management_system.pojo.Book;
import com.zhu.library_management_system.repository.BookRepository;
import com.zhu.library_management_system.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BookServiceImpl implements BookService {


    @Autowired
    BookRepository bookRepository;

    @Override
    public Iterable<Book> findAllBooks() {
        return bookRepository.findAll();
    }

    @Override
    public Book findBookById(Integer id) {
        return bookRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("书籍未找到，ID: " + id));
    }

    @Override
    public Book findBookByTitle(String title) {
        return bookRepository.findByTitle(title);
    }

    @Override
    public void updateBook(Book book) {
        bookRepository.save(book);
    }

    @Override
    public void deleteBook(Integer id) {
        bookRepository.deleteById(id);
    }

    @Override
    public void addBook(Book book) {
        bookRepository.save(book);
    }

    @Override
    public String getBookStatus(Integer bookId) {
        return bookRepository.getBookStatus(bookId);
    }

    @Override
    @Transactional
    public void updateBookStatus(Integer bookId, String bookStatus) {
        bookRepository.updateBookStatus(bookId,bookStatus);
    }

    @Override
    public Integer findStock(Integer id) {
        return bookRepository.findStock(id);
    }

    @Override
    @Transactional
    public void stock(Integer id, Integer stock) {
        bookRepository.stock(id,stock);
    }
}
